from PyQt6.QtWidgets import (QDialog, QVBoxLayout, QHBoxLayout, QLabel, 
                            QComboBox, QSpinBox, QCheckBox, QPushButton, 
                            QGroupBox, QFormLayout, QTabWidget)
from PyQt6.QtCore import Qt, QSettings
from PyQt6.QtGui import QFont

class SettingsDialog(QDialog):
    """设置对话框，用于管理应用程序设置"""
    
    def __init__(self, parent=None):
        super().__init__(parent)
        self.settings = QSettings('PasswordManager', 'Settings')
        self.init_ui()
        self.load_settings()
        self.apply_styles()
    
    def init_ui(self):
        """初始化用户界面"""
        self.setWindowTitle("设置")
        self.setMinimumWidth(500)
        
        # 主布局
        main_layout = QVBoxLayout()
        main_layout.setSpacing(15)
        main_layout.setContentsMargins(20, 20, 20, 20)
        
        # 创建标签页
        tab_widget = QTabWidget()
        tab_widget.setObjectName("settingsTab")
        
        # 常规设置标签页
        general_tab = QWidget()
        general_layout = QVBoxLayout(general_tab)
        general_layout.setSpacing(15)
        
        # 主题设置组
        theme_group = QGroupBox("主题设置")
        theme_layout = QFormLayout()
        
        self.theme_combo = QComboBox()
        self.theme_combo.addItems(["浅色", "深色", "跟随系统"])
        theme_layout.addRow("主题:", self.theme_combo)
        
        theme_group.setLayout(theme_layout)
        general_layout.addWidget(theme_group)
        
        # 安全设置组
        security_group = QGroupBox("安全设置")
        security_layout = QFormLayout()
        
        self.auto_lock_spin = QSpinBox()
        self.auto_lock_spin.setRange(0, 60)
        self.auto_lock_spin.setSuffix(" 分钟")
        self.auto_lock_spin.setSpecialValueText("从不")
        security_layout.addRow("自动锁定:", self.auto_lock_spin)
        
        self.clear_clipboard_spin = QSpinBox()
        self.clear_clipboard_spin.setRange(0, 60)
        self.clear_clipboard_spin.setSuffix(" 秒")
        self.clear_clipboard_spin.setSpecialValueText("从不")
        security_layout.addRow("清除剪贴板:", self.clear_clipboard_spin)
        
        security_group.setLayout(security_layout)
        general_layout.addWidget(security_group)
        
        # 显示设置组
        display_group = QGroupBox("显示设置")
        display_layout = QFormLayout()
        
        self.show_toolbar_check = QCheckBox("显示工具栏")
        display_layout.addRow(self.show_toolbar_check)
        
        self.show_statusbar_check = QCheckBox("显示状态栏")
        display_layout.addRow(self.show_statusbar_check)
        
        self.show_grid_check = QCheckBox("显示表格网格")
        display_layout.addRow(self.show_grid_check)
        
        display_group.setLayout(display_layout)
        general_layout.addWidget(display_group)
        
        # 密码设置标签页
        password_tab = QWidget()
        password_layout = QVBoxLayout(password_tab)
        password_layout.setSpacing(15)
        
        # 密码生成器设置组
        generator_group = QGroupBox("密码生成器设置")
        generator_layout = QFormLayout()
        
        self.default_length_spin = QSpinBox()
        self.default_length_spin.setRange(4, 64)
        generator_layout.addRow("默认密码长度:", self.default_length_spin)
        
        self.include_uppercase_check = QCheckBox("包含大写字母")
        generator_layout.addRow(self.include_uppercase_check)
        
        self.include_lowercase_check = QCheckBox("包含小写字母")
        generator_layout.addRow(self.include_lowercase_check)
        
        self.include_numbers_check = QCheckBox("包含数字")
        generator_layout.addRow(self.include_numbers_check)
        
        self.include_symbols_check = QCheckBox("包含特殊符号")
        generator_layout.addRow(self.include_symbols_check)
        
        self.exclude_similar_check = QCheckBox("排除相似字符 (1, l, I, 0, O)")
        generator_layout.addRow(self.exclude_similar_check)
        
        generator_group.setLayout(generator_layout)
        password_layout.addWidget(generator_group)
        
        # 密码显示设置组
        display_password_group = QGroupBox("密码显示设置")
        display_password_layout = QFormLayout()
        
        self.mask_passwords_check = QCheckBox("在表格中隐藏密码")
        display_password_layout.addRow(self.mask_passwords_check)
        
        self.show_password_strength_check = QCheckBox("显示密码强度指示器")
        display_password_layout.addRow(self.show_password_strength_check)
        
        display_password_group.setLayout(display_password_layout)
        password_layout.addWidget(display_password_group)
        
        # 添加标签页
        tab_widget.addTab(general_tab, "常规")
        tab_widget.addTab(password_tab, "密码")
        
        main_layout.addWidget(tab_widget)
        
        # 按钮布局
        button_layout = QHBoxLayout()
        button_layout.setSpacing(10)
        
        self.cancel_btn = QPushButton("取消")
        self.cancel_btn.setObjectName("cancelButton")
        self.cancel_btn.clicked.connect(self.reject)
        
        self.save_btn = QPushButton("保存")
        self.save_btn.setObjectName("primaryButton")
        self.save_btn.clicked.connect(self.accept)
        
        button_layout.addWidget(self.cancel_btn)
        button_layout.addWidget(self.save_btn)
        
        main_layout.addLayout(button_layout)
        
        self.setLayout(main_layout)
    
    def load_settings(self):
        """从QSettings加载设置"""
        # 主题设置
        theme = self.settings.value('theme', '浅色')
        self.theme_combo.setCurrentText(theme)
        
        # 安全设置
        auto_lock = self.settings.value('auto_lock', 5, type=int)
        self.auto_lock_spin.setValue(auto_lock)
        
        clear_clipboard = self.settings.value('clear_clipboard', 10, type=int)
        self.clear_clipboard_spin.setValue(clear_clipboard)
        
        # 显示设置
        show_toolbar = self.settings.value('show_toolbar', True, type=bool)
        self.show_toolbar_check.setChecked(show_toolbar)
        
        show_statusbar = self.settings.value('show_statusbar', True, type=bool)
        self.show_statusbar_check.setChecked(show_statusbar)
        
        show_grid = self.settings.value('show_grid', True, type=bool)
        self.show_grid_check.setChecked(show_grid)
        
        # 密码生成器设置
        default_length = self.settings.value('password_length', 16, type=int)
        self.default_length_spin.setValue(default_length)
        
        include_uppercase = self.settings.value('include_uppercase', True, type=bool)
        self.include_uppercase_check.setChecked(include_uppercase)
        
        include_lowercase = self.settings.value('include_lowercase', True, type=bool)
        self.include_lowercase_check.setChecked(include_lowercase)
        
        include_numbers = self.settings.value('include_numbers', True, type=bool)
        self.include_numbers_check.setChecked(include_numbers)
        
        include_symbols = self.settings.value('include_symbols', True, type=bool)
        self.include_symbols_check.setChecked(include_symbols)
        
        exclude_similar = self.settings.value('exclude_similar', False, type=bool)
        self.exclude_similar_check.setChecked(exclude_similar)
        
        # 密码显示设置
        mask_passwords = self.settings.value('mask_passwords', True, type=bool)
        self.mask_passwords_check.setChecked(mask_passwords)
        
        show_strength = self.settings.value('show_password_strength', True, type=bool)
        self.show_password_strength_check.setChecked(show_strength)
    
    def save_settings(self):
        """保存设置到QSettings"""
        # 主题设置
        self.settings.setValue('theme', self.theme_combo.currentText())
        
        # 安全设置
        self.settings.setValue('auto_lock', self.auto_lock_spin.value())
        self.settings.setValue('clear_clipboard', self.clear_clipboard_spin.value())
        
        # 显示设置
        self.settings.setValue('show_toolbar', self.show_toolbar_check.isChecked())
        self.settings.setValue('show_statusbar', self.show_statusbar_check.isChecked())
        self.settings.setValue('show_grid', self.show_grid_check.isChecked())
        
        # 密码生成器设置
        self.settings.setValue('password_length', self.default_length_spin.value())
        self.settings.setValue('include_uppercase', self.include_uppercase_check.isChecked())
        self.settings.setValue('include_lowercase', self.include_lowercase_check.isChecked())
        self.settings.setValue('include_numbers', self.include_numbers_check.isChecked())
        self.settings.setValue('include_symbols', self.include_symbols_check.isChecked())
        self.settings.setValue('exclude_similar', self.exclude_similar_check.isChecked())
        
        # 密码显示设置
        self.settings.setValue('mask_passwords', self.mask_passwords_check.isChecked())
        self.settings.setValue('show_password_strength', self.show_password_strength_check.isChecked())
        
        self.settings.sync()
    
    def accept(self):
        """保存设置并关闭对话框"""
        self.save_settings()
        super().accept()
    
    def apply_styles(self):
        """应用样式到对话框"""
        self.setStyleSheet("""
            QDialog {
                background-color: #f8f9fa;
                border-radius: 15px;
            }
            
            QGroupBox {
                font-weight: bold;
                border: 1px solid #e9ecef;
                border-radius: 8px;
                margin-top: 12px;
                padding-top: 24px;
                background-color: white;
            }
            
            QGroupBox::title {
                subcontrol-origin: margin;
                subcontrol-position: top left;
                left: 12px;
                padding: 0 5px;
                color: #495057;
                background-color: white;
            }
            
            QTabWidget::pane {
                border: 1px solid #e9ecef;
                border-radius: 8px;
                background-color: white;
            }
            
            QTabBar::tab {
                background-color: #f8f9fa;
                border: 1px solid #e9ecef;
                border-bottom: none;
                border-top-left-radius: 8px;
                border-top-right-radius: 8px;
                padding: 8px 16px;
                margin-right: 2px;
                color: #495057;
            }
            
            QTabBar::tab:selected {
                background-color: white;
                border-bottom: 2px solid #4dabf7;
                font-weight: bold;
            }
            
            QTabBar::tab:hover:!selected {
                background-color: #e9ecef;
            }
            
            QSpinBox, QComboBox {
                padding: 6px;
                border: 1px solid #ced4da;
                border-radius: 6px;
                background-color: white;
            }
            
            QSpinBox:focus, QComboBox:focus {
                border-color: #4dabf7;
                box-shadow: 0 0 0 3px rgba(77, 171, 247, 0.25);
            }
            
            QSpinBox:hover:!focus, QComboBox:hover:!focus {
                border-color: #adb5bd;
            }
            
            QCheckBox {
                spacing: 8px;
                color: #495057;
            }
            
            QCheckBox::indicator {
                width: 18px;
                height: 18px;
                border-radius: 4px;
                border: 1px solid #ced4da;
            }
            
            QCheckBox::indicator:unchecked {
                background-color: white;
            }
            
            QCheckBox::indicator:checked {
                background-color: #4dabf7;
                border-color: #4dabf7;
            }
            
            QCheckBox::indicator:hover {
                border-color: #339af0;
            }
            
            #primaryButton {
                background-color: #4dabf7;
                color: white;
                border: none;
                border-radius: 8px;
                padding: 10px 20px;
                font-weight: bold;
                font-size: 14px;
            }
            
            #primaryButton:hover {
                background-color: #339af0;
            }
            
            #primaryButton:pressed {
                background-color: #228be6;
            }
            
            #cancelButton {
                background-color: #f8f9fa;
                color: #495057;
                border: 1px solid #ced4da;
                border-radius: 8px;
                padding: 10px 20px;
                font-size: 14px;
            }
            
            #cancelButton:hover {
                background-color: #e9ecef;
                border-color: #adb5bd;
            }
            
            #cancelButton:pressed {
                background-color: #dee2e6;
            }
        """)